package com.spzx.common.rabbit.service;

import com.alibaba.fastjson2.JSON;
import com.spzx.common.core.utils.uuid.UUID;
import com.spzx.common.rabbit.entity.GuiguCorrelationData;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RabbitService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 发送消息
     *
     * @param exchange   交换机
     * @param routingKey 路由键
     * @param message    消息
     */
    public boolean sendMessage(String exchange, String routingKey, Object message) {
        rabbitTemplate.convertAndSend(exchange, routingKey, message);
        return true;
    }

    public boolean sendRetryMessage(String exchange, String routingKey, Object message) {
        GuiguCorrelationData correlationData = new GuiguCorrelationData();
        String uuid = "mq:" + UUID.randomUUID().toString().replaceAll("-", " ");
        correlationData.setId(uuid);
        correlationData.setExchange(exchange);
        correlationData.setRoutingKey(routingKey);
        correlationData.setMessage(message);
        correlationData.setDelay(false);
        correlationData.setRetryCount(3);
        rabbitTemplate.convertAndSend(exchange, routingKey, message, correlationData);
        redisTemplate.opsForValue().set(uuid, JSON.toJSONString(correlationData));
        return true;
    }

    public boolean sendDeadMessage(String exchange, String routingKey, Object message) {
        GuiguCorrelationData correlationData = new GuiguCorrelationData();
        String uuid = "mq:" + UUID.randomUUID().toString().replaceAll("-", " ");
        correlationData.setId(uuid);
        correlationData.setExchange(exchange);
        correlationData.setRoutingKey(routingKey);
        correlationData.setMessage(message);
        correlationData.setDelay(false);
        correlationData.setRetryCount(3);
        redisTemplate.opsForValue().set(uuid, JSON.toJSONString(correlationData));
        rabbitTemplate.convertAndSend(exchange, routingKey, message, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration(5*1000+"");
                return message;
            }
        },correlationData);
        return true;
    }

    public void sendDelayMessage(String exchange, String routingKey, String message, int delayTime) {
        GuiguCorrelationData guiguCorrelationData = new GuiguCorrelationData();
        String gid = UUID.randomUUID().toString().replace("-", "");
        guiguCorrelationData.setId(gid);
        guiguCorrelationData.setMessage(message);
        guiguCorrelationData.setDelay(true);// 是否延迟
        guiguCorrelationData.setRetryCount(3);// 默认重试次数，没重试一次应该-1
        guiguCorrelationData.setExchange(exchange);
        guiguCorrelationData.setRoutingKey(routingKey);
        // 保存消息副本到缓存，用于投递失败后重试
        redisTemplate.opsForValue().set(gid, JSON.toJSONString(guiguCorrelationData));
        rabbitTemplate.convertAndSend(exchange, routingKey, message, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelay(delayTime*1000);// TTL=5
                return message;
            }
        }, guiguCorrelationData);
    }

}